﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Policy;
using System.Security.Principal;
using System.Text;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Ink;
using System.Windows.Media;
using System.Xml.Serialization;
using BitsOfStuff.grafityMode;
using BitsOfStuff.MyActions;

//namespace Scrib
//{
//    public class Form1 : System.Windows.Forms.Form
//    {
//        private System.ComponentModel.Container components = null;
//        private Label label1 = null;
//        private Recognizer recognizer = null;
//        private Brush myBrush;

//        public Point previousPoint; // the last mouse pt in the
//        bool stylusDown = false;

//        public Form1()
//        {
//            this.components = new System.ComponentModel.Container();
//            this.Size = new System.Drawing.Size(300, 300);
//            this.Text = "Scribble.net";
//            this.BackColor = Color.White;
//            this.MouseDown +=
//                new System.Windows.Forms.MouseEventHandler(this.Form_MouseDown);
//            this.MouseMove += new MouseEventHandler(MouseMoveHandler);
//            this.MouseUp += new MouseEventHandler(MouseUpHandler);

//            label1 = new System.Windows.Forms.Label();
//            label1.Size = new System.Drawing.Size(200, 20);
//            label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
//            label1.Text = "First Name:";
//            this.Controls.Add(label1);

//            this.SuspendLayout();
//            this.Load += new System.EventHandler(this.Form1_Load);

//            myBrush = new System.Drawing.SolidBrush(Color.Black);

//            recognizer = new AlphaRecognizer();
//            recognizer.Initialize();
//            label1.Text = recognizer.Verify();
//        }

//        [STAThread]
//        private static void Main()
//        {
//            Application.Run(new Form1());
//        }

//        private void Form_MouseDown(object sender,
//                                    System.Windows.Forms.MouseEventArgs e)
//        {
//            stylusDown = true;

//            try
//            {
//                Point p = new Point(e.X, e.Y);
//                Graphics g = this.CreateGraphics();
//                g.Clear(Color.White);
//                g.FillEllipse(myBrush, e.X - 5, e.Y - 5, 10, 10);
//                previousPoint = p;
//                recognizer.AddPoint(p, true);
//            }
//            catch (Exception ex)
//            {
//                MessageBox.Show(ex.ToString());
//            }
//        }

//        private void MouseMoveHandler(Object sender, MouseEventArgs e)
//        {
//            try
//            {
//                if (stylusDown)
//                {
//                    Point p = new Point(e.X, e.Y);
//                    Graphics g = this.CreateGraphics();

//                    for (int i = 1; i <= 29; i++)
//                    {
//                        g.FillEllipse(myBrush, (p.X * i + previousPoint.X * (30 - i)) / 30 - 2, (p.Y * i + previousPoint.Y * (30 - i)) / 30 - 2, 4, 4);
//                    }

//                    previousPoint = p;
//                    recognizer.AddPoint(p, false);
//                }
//            }
//            catch (Exception ex)
//            {
//                MessageBox.Show(ex.ToString());
//            }
//        }

//        private void MouseUpHandler(Object sender, MouseEventArgs e)
//        {
//            stylusDown = false;
//            label1.Text = recognizer.Recognize();
//        }

//        private void Form1_Load(object sender, System.EventArgs e)
//        {
//            MainMenu mainMenu = new MainMenu();
//            this.Menu = mainMenu;
//            ContextMenu label1ContextMenu = new ContextMenu();
//            Label label1 = new Label();
//            label1.ContextMenu = label1ContextMenu;
//            MenuItem miFile = mainMenu.MenuItems.Add("&Menu Example");
//            miFile.MenuItems.Add(new MenuItem("&Item Example", new
//                                              EventHandler(this.Item_clicked)));
//        }

//        private void Item_clicked(object sender, System.EventArgs e)
//        {
//        }
//    }
//}

namespace BitsOfStuff
{
    public class GrafityRecognize
    {
        public GrafityRecognize(InkCanvas inkCanvas)
        {
            _inkCanvas = inkCanvas;
            recognizer = new AlphaRecognizer();
            recognizer.Initialize();
        }

        private grafityMode.Recognizer recognizer;

        private readonly InkCanvas _inkCanvas;

        public void analyze(StrokeCollection strokes)
        {
            //_myCharRecognizer.test();
            Stroke stroke = strokes.Last();
            MyPoint[] strokeInMypoints = SupportFunctions.makeMyPointsToArray(stroke);
            try
            {
                foreach (MyPoint strokeInMypoint in strokeInMypoints)
                {
                    recognizer.AddPoint(strokeInMypoint, false);
                }
                showTextBox(recognizer.Recognize().ToArray()[0]);
            }
            catch (Exception ex)
            {
                showTextBox(ex.ToString().ToArray()[0]);
            }
        }

        private void showTextBox(char c)
        {
            if (_inkCanvas.Dispatcher.CheckAccess())
            {
                showTextBoxSafe(c);
            }
            else
            {
                _inkCanvas.Dispatcher.Invoke(new Action(() => showTextBoxSafe(c)));
            }
        }

        [STAThread]
        private RichTextBox makeTextBox()
        {
            RichTextBox myTextBox = new RichTextBox();

            myTextBox.Height = 200;
            myTextBox.Width = 200;
            myTextBox.FontSize = 150;
            myTextBox.FontFamily = new FontFamily("Segoe");
            Color color = new Color();
            color.A = 153;
            color.R = 59;
            color.G = 55;
            color.B = 55;
            myTextBox.Foreground = new SolidColorBrush(color);
            Brush backgraund = new DrawingBrush();
            backgraund.Opacity = 0;
            myTextBox.BorderBrush = backgraund;
            myTextBox.Background = backgraund;

            return myTextBox;
        }

        [STAThread]
        private void showTextBoxSafe(char c)
        {
            var myTextBox = makeTextBox();
            myTextBox.AppendText(c.ToString(CultureInfo.InvariantCulture));
            SupportDrawnFunctions.reDrawnInkCanvas(_inkCanvas);
            //Can acees on curent thread
            InkCanvas.SetLeft(myTextBox, 300);
            InkCanvas.SetTop(myTextBox, 50);
            _inkCanvas.Children.Add(myTextBox);
        }
    }
}